'use strict';
const defaults = require('../defaults');
const { multipleWhitespacesToSingle, sanitizeFilename } = require('./utils');

/**
 * Add a track to the playlist file content.
 *
 * @param {String} saveFilePath
 * @param {Object} trackInfos
 */
const addTrackToPlaylist = (saveFilePath, trackInfos, move = true) => {
  if (defaults.PLAYLIST_FILE_ITEMS != null) {
    let saveFilePathForPlaylist = saveFilePath.replace(/\\+/g, '/');

    let artistName = multipleWhitespacesToSingle(
      sanitizeFilename(trackInfos.ALB_ART_NAME || trackInfos.ART_NAME)
    ).trim();

    if (!artistName) {
      artistName = 'Unknown artist';
    }

    defaults.PLAYLIST_FILE_ITEMS[trackInfos.SNG_ID] = {
      trackTitle: trackInfos.SNG_TITLE_VERSION,
      trackArtist: artistName,
      trackDuration: trackInfos.DURATION,
      trackSavePath: saveFilePathForPlaylist,
      move,
    };
  }
};

module.exports = addTrackToPlaylist;
